<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="Lawted&#39;s Blog">
<meta property="og:url" content="http://example.com/page/3/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Lawted Wu">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/page/3/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'en'
  };
</script>
<title>Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/08/JS%20U%20DONT%20KNOW%201/%E6%95%B0%E7%BB%84%E8%B6%8A%E7%95%8C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/08/JS%20U%20DONT%20KNOW%201/%E6%95%B0%E7%BB%84%E8%B6%8A%E7%95%8C/" class="post-title-link" itemprop="url">数组越界</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-08 21:51:26" itemprop="dateCreated datePublished" datetime="2021-04-08T21:51:26+08:00">2021-04-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:32:29" itemprop="dateModified" datetime="2021-05-07T09:32:29+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="数组越界"><a href="#数组越界" class="headerlink" title="数组越界"></a>数组越界</h2><p>JS中并不会数组越界，如果超过数组的范围会是<code>undefined</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="built_in">console</span>.log(arr[<span class="number">100</span>]) <span class="comment">//undefined</span></span><br></pre></td></tr></table></figure>
<p>JS的数组长度虽然是动态的但是，会有最大值，超过最大值会出现<code>RangeError</code></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/08/JS%20U%20DONT%20KNOW%201/null%E4%B8%8D%E6%98%AF%E5%AF%B9%E8%B1%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/08/JS%20U%20DONT%20KNOW%201/null%E4%B8%8D%E6%98%AF%E5%AF%B9%E8%B1%A1/" class="post-title-link" itemprop="url">null不是对象</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-08 21:50:14" itemprop="dateCreated datePublished" datetime="2021-04-08T21:50:14+08:00">2021-04-08</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:25" itemprop="dateModified" datetime="2021-05-07T09:31:25+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h4 id="null并不是对象："><a href="#null并不是对象：" class="headerlink" title="null并不是对象："></a>null并不是对象：</h4><p><code>null</code>其实并不是一个对象，尽管<code>typeof null</code> 输出的是<code>object</code>,但是这其实是一个bug。在js最初的版本中使用的是32位系统，为了性能考虑地位存储变量的类型信息，<code>000</code>开头表示为对象类型，然而<code>null</code>为全0，故而<code>null</code>被判断为对象类型。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/05/JS%20U%20DONT%20KNOW%201/this%E6%8C%87%E5%90%91%E8%AF%A6%E8%A7%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/05/JS%20U%20DONT%20KNOW%201/this%E6%8C%87%E5%90%91%E8%AF%A6%E8%A7%A3/" class="post-title-link" itemprop="url">this指向详解</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-05 21:00:56" itemprop="dateCreated datePublished" datetime="2021-04-05T21:00:56+08:00">2021-04-05</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:31" itemprop="dateModified" datetime="2021-05-07T09:31:31+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="1-默认绑定"><a href="#1-默认绑定" class="headerlink" title="1. 默认绑定"></a>1. 默认绑定</h2><p>默认绑定就是其他绑定都无法应用的时候就是默认绑定</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">3</span>;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line">foo(); <span class="comment">//2</span></span><br><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>函数调用了<code>this</code>的默认绑定，因此<code>this</code>指向全局对象，但是如果再<code>严格模式</code>中，<code>this</code>会指向<code>undefined</code></p>
<p><strong><code>this</code>只和调用位置有关，和声明位置无关，和词法作用域不一样！！！</strong></p>
<h2 id="2-隐式绑定"><a href="#2-隐式绑定" class="headerlink" title="2. 隐式绑定"></a>2. 隐式绑定</h2><p>调用位置有上下文对象，也就是被某个对象拥有或者包含</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">2</span>,</span><br><span class="line">    foo:foo</span><br><span class="line">&#125;;</span><br><span class="line">obj.foo();</span><br></pre></td></tr></table></figure>
<p>可以说<code>foo()</code>被调用的时候，<code>obj</code>包含它，拥有它,当使用函数别名的时候容易发生<code>丢失绑定对象</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">	a:<span class="number">2</span>,</span><br><span class="line">    foo:foo</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = obj.foo; <span class="comment">//使用了函数别名</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;global&quot;</span>;</span><br><span class="line">bar(); <span class="comment">//&quot;global&quot;</span></span><br></pre></td></tr></table></figure>
<p><code>bar()</code>引用了<code>foo</code>函数本身，所以没有绑定对象，因此是一个默认对象，<code>丢失绑定对象</code>还会发生再传入回调函数的时候</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doFoo</span>(<span class="params">fn</span>)</span>&#123;</span><br><span class="line">    fn();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">	a:<span class="number">2</span>,</span><br><span class="line">    foo:foo</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;global&quot;</span>;</span><br><span class="line">doFoo(obj.foo);</span><br></pre></td></tr></table></figure>
<p>常见的<code>setTimeout</code>其实是这样的</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setTimeout</span>(<span class="params">fn,delay</span>)</span>&#123;</span><br><span class="line">	<span class="comment">//wait delay</span></span><br><span class="line">    fn();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>所以<code>setTimeout</code>会指向全局<code>window</code></p>
<h2 id="3-显式绑定"><a href="#3-显式绑定" class="headerlink" title="3. 显式绑定"></a>3. 显式绑定</h2><p>使用<code>call</code>和<code>aplly</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">foo.call(obj);<span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<h3 id="1-硬绑定"><a href="#1-硬绑定" class="headerlink" title="1. 硬绑定"></a>1. 硬绑定</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	foo.call(obj);</span><br><span class="line">&#125;</span><br><span class="line">bar();</span><br><span class="line"><span class="built_in">setTimeout</span>(bar,<span class="number">100</span>);</span><br><span class="line">bar.call(<span class="built_in">window</span>);</span><br></pre></td></tr></table></figure>
<p>创建了<code>bar</code>函数，函数<code>foo</code>的内部直接写死<code>call obj</code>对象，因此这是一种显式的强制绑定，称为硬绑定</p>
<p><code>硬绑定</code>常用的一个场景就是包裹函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">something</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.a,something);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.a + something;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> foo.apply(obj,<span class="built_in">arguments</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> b = bar(<span class="number">3</span>); <span class="comment">//2 3</span></span><br><span class="line"><span class="built_in">console</span>.log(b); <span class="comment">//5</span></span><br></pre></td></tr></table></figure>
<p>相当于传进去的参数自动和<code>obj.a</code>做加法</p>
<p>另一种方法就是创建一个可以重复使用的辅助函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">something</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a,something);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.a + something;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">fn,obj</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">return</span> fn.apply(obj,<span class="built_in">arguments</span>);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">	a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = bind(foo,obj);<span class="comment">//绑定需要的对象</span></span><br><span class="line"><span class="keyword">var</span> b = bar(<span class="number">3</span>) <span class="comment">//2 3</span></span><br><span class="line"><span class="built_in">console</span>.log(b); <span class="comment">//5</span></span><br></pre></td></tr></table></figure>
<p>这就是<code>bind</code>，返回一个新的函数，然后把<code>this</code>设置为上下文</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">something</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a,something);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.a + something;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">	a:<span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = foo.bind(obj);</span><br><span class="line"><span class="keyword">var</span> b = bar(<span class="number">3</span>); <span class="comment">//2 3</span></span><br><span class="line"><span class="built_in">console</span>.log(b); <span class="comment">//5</span></span><br></pre></td></tr></table></figure>


<h2 id="4-new绑定"><a href="#4-new绑定" class="headerlink" title="4.new绑定"></a>4.new绑定</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">a</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">this</span>.a = a</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = <span class="keyword">new</span> foo(<span class="number">2</span>);</span><br><span class="line"><span class="built_in">console</span>.log(bar.a);</span><br></pre></td></tr></table></figure>
<p><code>new</code>的四步操作</p>
<ol>
<li>创建一个新的对象</li>
<li>新的对象进行<code>__proto__</code>连接</li>
<li>绑定<code>this</code>到新的创建的对象</li>
<li>如果没有返回别的对象，则返回<code>this</code>（新创建的对象）</li>
</ol>
<p><code>bar</code>是从<code>foo</code>中<code>new</code>出来的，所以这个<code>this.a</code>就是<code>bar.a</code>，等同于把<code>bar</code>新增了一个属性<code>a</code>,如果<code>this</code>指向的不是<code>bar</code>，那么会输出<code>undefined</code></p>
<h2 id="5-优先级"><a href="#5-优先级" class="headerlink" title="5.优先级"></a>5.优先级</h2><p>显式绑定✔和隐形绑定哪个优先级更高？</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj1 = &#123;</span><br><span class="line">    a:<span class="number">2</span>,</span><br><span class="line">    foo:foo</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj2 = &#123;</span><br><span class="line">    a:<span class="number">3</span>,</span><br><span class="line">    foo:foo</span><br><span class="line">&#125;</span><br><span class="line">obj1.foo(); <span class="comment">//2</span></span><br><span class="line">obj2.foo(); <span class="comment">//3</span></span><br><span class="line"></span><br><span class="line">obj1.foo.call(obj2); <span class="comment">//3</span></span><br><span class="line">obj2.foo.call(obj1); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p><strong>因为<code>obj1.foo.call(obj2);</code>输出是<code>3</code>,所以显示绑定的优先级更高</strong></p>
<p><code>new</code>绑定和隐式绑定的优先级谁高</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">something</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.a = something</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj1 = &#123;</span><br><span class="line">    foo:foo</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj2 = &#123;&#125;;</span><br><span class="line"></span><br><span class="line">obj1.foo(<span class="number">2</span>);</span><br><span class="line"><span class="built_in">console</span>.log(obj1.a); <span class="comment">//2</span></span><br><span class="line"></span><br><span class="line">obj1.foo.call(obj2,<span class="number">3</span>);</span><br><span class="line"><span class="built_in">console</span>.log(obj2.a); <span class="comment">//3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> bar = <span class="keyword">new</span> obj1.foo(<span class="number">4</span>);</span><br><span class="line"><span class="built_in">console</span>.log(obj1.a); <span class="comment">//2</span></span><br><span class="line"><span class="built_in">console</span>.log(bar.a); <span class="comment">//4</span></span><br></pre></td></tr></table></figure>
<p><code>new</code>绑定比隐式绑定优先级高，而且<code>new</code>绑定比显式绑定优先级更高</p>
<h2 id="6-判断this"><a href="#6-判断this" class="headerlink" title="6. 判断this"></a>6. 判断this</h2><ol>
<li>函数是否在<code>new</code>中调用？<code>this</code>绑定的是新创建的对象</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> bar = <span class="keyword">new</span> foo();</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>函数是否通过<code>call/apply</code>或者硬绑定调用？<code>this</code>绑定的是指向的对象</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> bar = foo.call(obj2)</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>函数是否在某个上下文对象中调用？<code>this</code>绑定是上下文对象</li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> bar = obj1.foo()</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>默认绑定，如果是严格模式就是<code>undefined</code>,非严格模式下就绑定到<code>全局对象</code></li>
</ol>
<h3 id="6-绑定例外"><a href="#6-绑定例外" class="headerlink" title="6. 绑定例外"></a>6. 绑定例外</h3><h3 id="被忽略的this"><a href="#被忽略的this" class="headerlink" title="被忽略的this"></a>被忽略的<code>this</code></h3><p>如果把<code>null</code>或者<code>undefined</code>作为<code>this</code>的绑定对象传入<code>call</code>，<code>apply</code>或者<code>bind</code>，这些值会被忽略实际应用的是<code>默认绑定</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line">foo.call(<span class="literal">null</span>); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>在柯里化的时候可以运用<code>null</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">&quot;a:&quot;</span> + a + <span class="string">&quot;, b:&quot;</span> + b);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = foo.bind(<span class="literal">null</span>,<span class="number">2</span>);</span><br><span class="line">bar(<span class="number">3</span>); <span class="comment">//a:2 b:3</span></span><br></pre></td></tr></table></figure>
<p>如果函数并不关心<code>this</code>的话，但是依然需要传入一个占位，这时就可以用<code>null</code></p>
<h3 id="间接引用"><a href="#间接引用" class="headerlink" title="间接引用"></a>间接引用</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="built_in">this</span>.a);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line"><span class="keyword">var</span> o = &#123;<span class="attr">a</span>:<span class="number">3</span>,<span class="attr">foo</span>:foo&#125;;</span><br><span class="line"><span class="keyword">var</span> p = &#123;<span class="attr">a</span>:<span class="number">4</span>&#125;;</span><br><span class="line"></span><br><span class="line">o.foo(); <span class="comment">//3</span></span><br><span class="line">(p.foo = o.foo)(); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>因为<code>(p.foo = o.foo)();</code>的匿名函数<code>this</code>指向全局，所以返回的是全局的<code>a</code></p>
<h2 id="this词法-箭头函数作用域"><a href="#this词法-箭头函数作用域" class="headerlink" title="this词法(箭头函数作用域)"></a>this词法(箭头函数作用域)</h2><p>看看箭头函数的词法作用域</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function">(<span class="params">a</span>) =&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.a)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj1 = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj2 = &#123;</span><br><span class="line">    a:<span class="number">3</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> bar = foo.call(obj1)</span><br><span class="line">bar.call(obj2) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>箭头函数在调用时会捕获<code>foo()</code>的<code>this</code>,由于<code>foo()</code>的<code>this</code>绑定到<code>obj1</code>,而<strong>箭头函数的绑定无法修改</strong>,所以<code>bar</code>也绑定到<code>obj2</code></p>
<p>箭头函数最常用于回调函数,确保函数的<code>this</code>被绑定到指定对象</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.a)</span><br><span class="line">    &#125;,<span class="number">100</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">foo.call(obj) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>


<h2 id="7-小结"><a href="#7-小结" class="headerlink" title="7.小结"></a>7.小结</h2><ol>
<li><p>函数是否在<code>new</code>中调用？<code>this</code>绑定的是新创建的对象</p>
</li>
<li><p>函数是否通过<code>call/apply</code>或者硬绑定调用？<code>this</code>绑定的是指向的对象</p>
</li>
<li><p>函数是否在某个上下文对象中调用？<code>this</code>绑定是上下文对象</p>
</li>
<li><p>默认绑定，如果是严格模式就是<code>undefined</code>,非严格模式下就绑定到<code>全局对象</code></p>
</li>
</ol>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/05/JS%20U%20DONT%20KNOW%201/%E7%AE%80%E5%8D%95%E8%AF%B4%E8%AF%B4this/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/05/JS%20U%20DONT%20KNOW%201/%E7%AE%80%E5%8D%95%E8%AF%B4%E8%AF%B4this/" class="post-title-link" itemprop="url">简单说说this</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-05 13:53:40" itemprop="dateCreated datePublished" datetime="2021-04-05T13:53:40+08:00">2021-04-05</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:32:05" itemprop="dateModified" datetime="2021-05-07T09:32:05+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="this"><a href="#this" class="headerlink" title="this"></a>this</h2><p><code>匿名函数</code>,<code>setTimeout</code>,<code>setInterval</code>的this都是指向<code>window(非严格模式)</code>,<code>undefined(严格模式)</code></p>
<h4 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;window&#x27;</span>; </span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> A = &#123;</span><br><span class="line">   name: <span class="string">&#x27;A&#x27;</span>,</span><br><span class="line">   sayHello: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="comment">//这是一个具名函数，this指向上下文</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">this</span>.name)</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">A.sayHello();<span class="comment">// A</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;window&#x27;</span>; </span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> A = &#123;</span><br><span class="line">   name: <span class="string">&#x27;A&#x27;</span>,</span><br><span class="line">   sayHello: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="comment">//这是一个匿名函数，this指向window</span></span><br><span class="line">       <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	      <span class="built_in">console</span>.log(<span class="built_in">this</span>.name)</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">A.sayHello()();<span class="comment">// window</span></span><br></pre></td></tr></table></figure>
<h4 id="setTimeout"><a href="#setTimeout" class="headerlink" title="setTimeout"></a>setTimeout</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;window&#x27;</span>; </span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> A = &#123;</span><br><span class="line">   name: <span class="string">&#x27;A&#x27;</span>,</span><br><span class="line">   sayHello: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">       <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="built_in">this</span>.name)&#125;,<span class="number">0</span>)<span class="comment">//this指向window</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">A.sayHello();<span class="comment">// window</span></span><br></pre></td></tr></table></figure>
<p>如果我么使用箭头函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;window&#x27;</span>; </span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> A = &#123;</span><br><span class="line">   name: <span class="string">&#x27;A&#x27;</span>,</span><br><span class="line">   sayHello: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">       <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;<span class="built_in">console</span>.log(<span class="built_in">this</span>.name)&#125;,<span class="number">0</span>)<span class="comment">//这是一个箭头函数，this就是sayhello的this</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">A.sayHello();<span class="comment">// A</span></span><br></pre></td></tr></table></figure>
<p>箭头函数的<code>this</code>指向是<code>父级函数作用域</code>（sayHello）的<code>this</code></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/05/JS%20U%20DONT%20KNOW%201/%E5%8A%A8%E6%80%81%E4%BD%9C%E7%94%A8%E5%9F%9F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/05/JS%20U%20DONT%20KNOW%201/%E5%8A%A8%E6%80%81%E4%BD%9C%E7%94%A8%E5%9F%9F/" class="post-title-link" itemprop="url">动态作用域</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-05 13:28:36" itemprop="dateCreated datePublished" datetime="2021-04-05T13:28:36+08:00">2021-04-05</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:48" itemprop="dateModified" datetime="2021-05-07T09:31:48+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="动态作用域"><a href="#动态作用域" class="headerlink" title="动态作用域"></a>动态作用域</h2><p>因为<code>javaScript</code>采用的是<strong>词法作用域</strong>,函数的作用域在函数<strong>定义</strong>的时候就决定了。<br>而词法作用域相对的是<strong>动态作用域</strong>,函数的作用域是在函数<strong>调用</strong>的时候才决定的。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log( a ); <span class="comment">//2,词法作用域</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bar</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> a = <span class="number">3</span>;</span><br><span class="line">    foo();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line">bar();</span><br></pre></td></tr></table></figure>
<p>如果JavaScript 是<strong>动态作用域</strong>，那么他会输出<code>3</code>(事实上并不是！！！)，但是如果我们修改代码成这样，<code>foo()</code>再被创建的时候就已经确定了一个闭包他在<code>bar()</code>的内部</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bar</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> a = <span class="number">3</span>;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"> 	 <span class="built_in">console</span>.log(a);</span><br><span class="line">	&#125;</span><br><span class="line">    foo()</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line">bar(); <span class="comment">// 2;</span></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/04/JS%20U%20DONT%20KNOW%201/%E9%97%AD%E5%8C%85%E5%92%8C%E6%A8%A1%E5%9D%97%E5%8C%96/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/04/JS%20U%20DONT%20KNOW%201/%E9%97%AD%E5%8C%85%E5%92%8C%E6%A8%A1%E5%9D%97%E5%8C%96/" class="post-title-link" itemprop="url">闭包和模块化</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-04 21:06:34" itemprop="dateCreated datePublished" datetime="2021-04-04T21:06:34+08:00">2021-04-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:43" itemprop="dateModified" datetime="2021-05-07T09:31:43+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="闭包和模块化"><a href="#闭包和模块化" class="headerlink" title="闭包和模块化"></a>闭包和模块化</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">CoolModule</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> something = <span class="string">&quot;cool&quot;</span>;</span><br><span class="line">    <span class="keyword">var</span> another = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">doSomething</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(something);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">doAnother</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(another.join(<span class="string">&quot;!&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        doSomething:doSomething,<span class="comment">//doSomething是一个属性，类型是一个函数，所以调用foo.doSomething();</span></span><br><span class="line">        doAnother:doAnother<span class="comment">//同上</span></span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> foo = CoolModule();</span><br><span class="line">foo.doSomething(); <span class="comment">//cool</span></span><br><span class="line">foo.doAnother(); <span class="comment">//1!2!3</span></span><br></pre></td></tr></table></figure>
<p><code>CoolModule()</code>是一个函数,里面有闭包，他返回一个对象，对象含有内部函数的引用</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/04/%E9%97%AD%E5%8C%85%E5%B0%8F%E6%B5%8B%E8%AF%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/04/%E9%97%AD%E5%8C%85%E5%B0%8F%E6%B5%8B%E8%AF%95/" class="post-title-link" itemprop="url">闭包小测试</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>
      

      <time title="Created: 2021-04-04 20:42:40 / Modified: 20:43:11" itemprop="dateCreated datePublished" datetime="2021-04-04T20:42:40+08:00">2021-04-04</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fun</span>(<span class="params">n,o</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(o)</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    fun:<span class="function"><span class="keyword">function</span>(<span class="params">m</span>)</span>&#123;</span><br><span class="line">      <span class="keyword">return</span> fun(m,n);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = fun(<span class="number">0</span>);  a.fun(<span class="number">1</span>);  a.fun(<span class="number">2</span>);  a.fun(<span class="number">3</span>);<span class="comment">//undefined,?,?,?</span></span><br><span class="line"><span class="keyword">var</span> b = fun(<span class="number">0</span>).fun(<span class="number">1</span>).fun(<span class="number">2</span>).fun(<span class="number">3</span>);<span class="comment">//undefined,?,?,?</span></span><br><span class="line"><span class="keyword">var</span> c = fun(<span class="number">0</span>).fun(<span class="number">1</span>);  c.fun(<span class="number">2</span>);  c.fun(<span class="number">3</span>);<span class="comment">//undefined,?,?,?</span></span><br><span class="line"><span class="comment">//问:三行a,b,c的输出分别是什么？</span></span><br></pre></td></tr></table></figure>


<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//答案：</span></span><br><span class="line"><span class="comment">//a: undefined,0,0,0</span></span><br><span class="line"><span class="comment">//b: undefined,0,1,2</span></span><br><span class="line"><span class="comment">//c: undefined,0,1,1</span></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/04/JS%20U%20DONT%20KNOW%201/%E6%8C%89%E5%80%BC%E4%BC%A0%E5%8F%82/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/04/JS%20U%20DONT%20KNOW%201/%E6%8C%89%E5%80%BC%E4%BC%A0%E5%8F%82/" class="post-title-link" itemprop="url">按值传参</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-04 18:51:12" itemprop="dateCreated datePublished" datetime="2021-04-04T18:51:12+08:00">2021-04-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:37" itemprop="dateModified" datetime="2021-05-07T09:31:37+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="【JS】引用对象是如何传参的？"><a href="#【JS】引用对象是如何传参的？" class="headerlink" title="【JS】引用对象是如何传参的？"></a>【JS】引用对象是如何传参的？</h2><h3 id="先看两段代码JS"><a href="#先看两段代码JS" class="headerlink" title="先看两段代码JS"></a>先看两段代码JS</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 1.1</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line">add();</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">//ReferenceError</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 1.2</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	a = <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line">add();</span><br><span class="line"><span class="built_in">console</span>.log(a);<span class="comment">// 2</span></span><br></pre></td></tr></table></figure>
<p>第一个输出<code>ReferenceError</code>,第二个输出<code>2</code> ,因为在函数内部使用<code>var</code>则是一个局部变量，如果不使用<code>var</code>根据<a target="_blank" rel="noopener" href="https://lawted.gitee.io/2021/04/03/JS%20U%20DONT%20KNOW/%E7%90%86%E8%A7%A3%E4%BD%9C%E7%94%A8%E5%9F%9F/">理解作用域</a>中的<code>LHS</code>查找失败后，非严格模式下会自动创建全局变量，所以相当于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 1.3</span></span><br><span class="line"><span class="keyword">var</span> a;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	a = <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line">add();</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>


<h3 id="再看两段代码"><a href="#再看两段代码" class="headerlink" title="再看两段代码"></a>再看两段代码</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.1</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">	arg++;</span><br><span class="line">&#125;</span><br><span class="line">add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//0</span></span><br><span class="line"><span class="built_in">console</span>.log(arg); <span class="comment">//ReferenceError</span></span><br></pre></td></tr></table></figure>
<p>这段代码说明了如果基本类型传参，那么是按值传递，也就是复制了一份然后传给了<code>arg</code>,如果试试输出<code>arg</code>就会是<code>ReferenceError</code>，因为<code>arg</code>是形参，<strong>形参在函数结束后会会被释放</strong></p>
<p>这是基本类型的情况，再看看引用类型</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.2</span></span><br><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">name</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">	arg.name = <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line">add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//&#123;name: 2&#125;</span></span><br></pre></td></tr></table></figure>
<p>这个地方就和基本类型不同，看上去引用类型传参好像是引用传参？再看一个例子</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.3</span></span><br><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">name</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">    arg.tel = <span class="number">1</span></span><br><span class="line">&#125;</span><br><span class="line">add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//&#123;name: 1, tel: 1&#125;</span></span><br></pre></td></tr></table></figure>
<p>这里给<code>a</code>添加了一个属性<code>tel</code>,再看下一段代码</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.4</span></span><br><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">name</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">    arg.name = <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//&#123;name: 2&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">//&#123;name: 2&#125;</span></span><br></pre></td></tr></table></figure>
<p>好像确实是引用传值，因为<code>a</code>的<code>name</code>也被修改了,那么再来看一个能证明他不是引用传值的例子</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.5</span></span><br><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">name</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">    arg = &#123;<span class="attr">name</span>:<span class="number">2</span>&#125;</span><br><span class="line">    <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//&#123;name: 1&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">//&#123;name: 2&#125;</span></span><br></pre></td></tr></table></figure>
<p>如果引用类型的传参方式确实是引用传值，我们应该会修改<code>arg</code> 为<code>&#123;name:2&#125;</code>的时候<code>a</code>的<code>name</code>也会被修改，但是<code>a</code> 的<code>name</code>依旧是1，所以到底内部发生了什么呢？</p>
<h3 id="background"><a href="#background" class="headerlink" title="background"></a>background</h3><p>JavaScript中有堆内存和栈内存，<strong>栈内存中存放着基本类型的变量</strong> ，而<strong>引用类型则是再栈内存中一个指针，指向了堆内存</strong></p>
<p><img src="https://images2018.cnblogs.com/blog/461976/201808/461976-20180823211511434-1707579794.png" alt="img"></p>
<h3 id="解释上面的最后一段代码"><a href="#解释上面的最后一段代码" class="headerlink" title="解释上面的最后一段代码"></a>解释上面的最后一段代码</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.4</span></span><br><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">name</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">    arg.name = <span class="number">2</span>;</span><br><span class="line">    <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//&#123;name: 2&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">//&#123;name: 2&#125;</span></span><br></pre></td></tr></table></figure>
<p><code>a</code>是一个在栈内存里面的指针指向堆内存中的<code>object&#123;name:1&#125;</code>，使用<code>arg</code>作为形参的时候，<code>a</code>会把他的指向告诉<code>arg</code>。于是<code>arg</code>也指向堆内存中的<code>object&#123;name:1&#125;</code>，然后<code>arg</code>想要修改<code>name</code>的值为2，最后返回的都是他们指向的<code>object&#123;name:2&#125;</code>。所以打印出来的是两个指针有着相同的指向。接下来看<code>code 2.5</code></p>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/cKR2eH"><img src="https://z3.ax1x.com/2021/04/04/cKR2eH.png" alt="cKR2eH.png"></a></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//Code 2.5</span></span><br><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">name</span>:<span class="number">1</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">arg</span>)</span>&#123;</span><br><span class="line">    arg = &#123;<span class="attr">name</span>:<span class="number">2</span>&#125;</span><br><span class="line">    <span class="keyword">return</span> arg;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> res = add(a);</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">//&#123;name: 1&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">//&#123;name: 2&#125;</span></span><br></pre></td></tr></table></figure>
<p><code>a</code>是一个在栈内存里面的指针指向堆内存中的<code>object&#123;name:1&#125;</code>，使用<code>arg</code>作为形参的时候，<code>a</code>会把他的指向告诉<code>arg</code>。但是<code>arg</code>要创建一个新的对象，于是<code>arg</code>在堆内存里面找到一个创建<code>object&#123;name:2&#125;</code>，然后指向这个新创建的对象，所以打印出来的是两个指针有着不同的指向</p>
<h3 id="最后看一个常考面试题"><a href="#最后看一个常考面试题" class="headerlink" title="最后看一个常考面试题"></a>最后看一个常考面试题</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setObj</span>(<span class="params">p</span>) </span>&#123;</span><br><span class="line">    p.name=<span class="string">&quot;AA&quot;</span>;</span><br><span class="line">    p=&#123;&#125;;</span><br><span class="line">    p.name=<span class="string">&quot;BB&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> p=&#123;</span><br><span class="line">    name:<span class="string">&#x27;CC&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line">setObj(p);</span><br><span class="line"><span class="built_in">console</span>.log(p);</span><br></pre></td></tr></table></figure>
<p>答案是 <code>&#123;name: &quot;AA&quot;&#125;</code></p>
<p>下面来分析：</p>
<ul>
<li>首先有个在栈内存中的<code>p</code>他指向了堆内存中的<code>object&#123;name:&quot;CC&quot;&#125;</code></li>
<li>然后执行<code>setObj()</code>函数，<code>p</code>把自己的指向告诉了<code>形参p</code>,<code>形参p</code>也指向了<code>object&#123;name:&quot;CC&quot;&#125;</code>，接着直接修改成了<code>object&#123;name:&quot;AA&quot;&#125;</code></li>
<li><code>形参p</code>要创建一个对象，于是他指向了堆内存中的一个<code>object&#123;&#125;</code></li>
<li><code>形参p</code>修改这个他刚创建的对象为<code>object&#123;name:&quot;BB&quot;&#125;</code></li>
<li>函数结束，<code>形参p</code>被释放</li>
<li>打印出<code>p</code>的值为<code>&#123;name: &quot;AA&quot;&#125;</code></li>
</ul>
<p><a target="_blank" rel="noopener" href="https://imgtu.com/i/cKfIsS"><img src="https://z3.ax1x.com/2021/04/04/cKfIsS.jpg" alt="cKfIsS.jpg"></a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/04/JS%20U%20DONT%20KNOW%201/%E6%8F%90%E5%8D%87/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/04/JS%20U%20DONT%20KNOW%201/%E6%8F%90%E5%8D%87/" class="post-title-link" itemprop="url">提升</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-04 14:06:28" itemprop="dateCreated datePublished" datetime="2021-04-04T14:06:28+08:00">2021-04-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:32:42" itemprop="dateModified" datetime="2021-05-07T09:32:42+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="函数提升"><a href="#函数提升" class="headerlink" title="函数提升"></a>函数提升</h2><p>我们知道有变量提升</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//undefined</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br></pre></td></tr></table></figure>
<p>相当于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a;</span><br><span class="line"><span class="built_in">console</span>.log(a);</span><br><span class="line">a = <span class="number">2</span>;</span><br></pre></td></tr></table></figure>


<p>函数同样会被提升</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">foo();</span><br><span class="line">bar();</span><br><span class="line"><span class="keyword">var</span> foo = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> bar = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>相当于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> foo;</span><br><span class="line">foo(); <span class="comment">// RHS查询出现错误，抛出TypeError</span></span><br><span class="line">bar(); <span class="comment">// LHS查询出现错误，抛出ReferenceError</span></span><br><span class="line">foo = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> bar = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>




<h2 id="函数优先"><a href="#函数优先" class="headerlink" title="函数优先"></a>函数优先</h2><p>函数声明和变量声明都会被提升，但是会<strong>先提升函数，再提升变量</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">foo(); <span class="comment">// 1</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> foo;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line">foo = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">2</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>结果会输出<code>1</code>而不是<code>2</code></p>
<p>这段代码相当于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="number">1</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">foo(); <span class="comment">// 1</span></span><br><span class="line"></span><br><span class="line">foo = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">2</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>虽然有<code>var foo</code>但是函数<code>function foo()</code>被提升，覆盖了<code>var foo</code>的声明</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">foo(); <span class="comment">//3</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> foo = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="number">3</span>);</span><br><span class="line">&#125;</span><br><span class="line">foo(); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>相当于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="number">3</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">foo(); <span class="comment">//3</span></span><br><span class="line"></span><br><span class="line">foo = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">foo(); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/03/%E8%AF%8D%E6%B3%95%E4%BD%9C%E7%94%A8%E5%9F%9F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/03/%E8%AF%8D%E6%B3%95%E4%BD%9C%E7%94%A8%E5%9F%9F/" class="post-title-link" itemprop="url">词法作用域</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>
      

      <time title="Created: 2021-04-03 19:57:23 / Modified: 20:58:23" itemprop="dateCreated datePublished" datetime="2021-04-03T19:57:23+08:00">2021-04-03</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="词法作用域"><a href="#词法作用域" class="headerlink" title="词法作用域"></a>词法作用域</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">a</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> b = a * <span class="number">2</span>;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">bar</span>(<span class="params">c</span>)</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(a,b,c);</span><br><span class="line">    &#125;</span><br><span class="line">    bar(b*<span class="number">3</span>);</span><br><span class="line">&#125;</span><br><span class="line">foo(<span class="number">2</span>); <span class="comment">//2,4,12</span></span><br></pre></td></tr></table></figure>
<p>bar作用域创建的标识符，<code>c</code></p>
<p>foo作用域创建的标识符，<code>a</code>,<code>bar</code>，<code>b</code></p>
<p>全局作用域创建的标识符，<code>foo</code></p>
<p>首先从最内部的作用域，bar函数的作用域开始查找，引擎无法找到<code>a</code>，因此回去上一级foo中找，找到<code>b</code>和<code>c</code></p>
<h3 id="欺骗词法"><a href="#欺骗词法" class="headerlink" title="欺骗词法"></a>欺骗词法</h3><h4 id="eval"><a href="#eval" class="headerlink" title="eval"></a>eval</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">str,a</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">eval</span>(str);</span><br><span class="line">    <span class="built_in">console</span>.log(a,b);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> b = <span class="number">2</span>;</span><br><span class="line">foo(<span class="string">&quot;var b = 3&quot;</span>,<span class="number">1</span>) <span class="comment">//1，3</span></span><br></pre></td></tr></table></figure>
<p>eval中的<code>&quot;var b = 3&quot;</code>相当于本来就出现在那里一样处理，所以声明了b，对foo的词法作用域进行了修改，遮蔽了全局作用域的同名变量</p>
<h4 id="with"><a href="#with" class="headerlink" title="with"></a>with</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">1</span>,</span><br><span class="line">    b:<span class="number">2</span>,</span><br><span class="line">    c:<span class="number">3</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">with</span>(<span class="params">obj</span>)</span>&#123;</span><br><span class="line">	a = <span class="number">3</span>;</span><br><span class="line">    b = <span class="number">4</span>;</span><br><span class="line">    c = <span class="number">5</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj)</span><br></pre></td></tr></table></figure>
<p>使用with可以快捷的赋值obj中的局部变量</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params">obj</span>)</span>&#123;</span><br><span class="line">	<span class="function"><span class="title">with</span>(<span class="params">obj</span>)</span>&#123;</span><br><span class="line">        a=<span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> o1 = &#123;</span><br><span class="line">    a:<span class="number">3</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> o2 = &#123;</span><br><span class="line">    b:<span class="number">3</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//first</span></span><br><span class="line">foo(o1);</span><br><span class="line"><span class="built_in">console</span>.log(o1.a); <span class="comment">//2</span></span><br><span class="line"><span class="comment">//second</span></span><br><span class="line">foo(o2);</span><br><span class="line"><span class="built_in">console</span>.log(o2.a); <span class="comment">//undefined</span></span><br><span class="line"><span class="built_in">console</span>.log(a);	   <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p><code>first</code> 中，将o1.a进行了重新赋值，<code>second</code>中没有找到o2的属性a所以with创建了一个全局的变量，所以不推荐使用</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/2/"><i class="fa fa-angle-left" aria-label="Previous page"></i></a><a class="page-number" href="/">1</a><a class="page-number" href="/page/2/">2</a><span class="page-number current">3</span><a class="page-number" href="/page/4/">4</a><span class="space">&hellip;</span><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/4/"><i class="fa fa-angle-right" aria-label="Next page"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
